/*=====================================================================

  File:      StoreSimple.cs
  Summary:   SQL Remote Blob Storage sample application - StoreSimple.
             Sample application used to store data using all available 
             defaults.
             Blob Storage libraries.
  Date:      July 21st, 2008

---------------------------------------------------------------------

  This file is part of the Microsoft SQL Server Code Samples.
  Copyright (C) Microsoft Corporation.  All rights reserved.

This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.

THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

======================================================= */

using System;
using System.Data.SqlClient;
using System.Text;
using System.Transactions;

using Microsoft.Data.SqlRemoteBlobs;

namespace Microsoft.Samples.RemoteBlobStorage
{
    /// <summary>
    /// Sample Remote Blob Storage Application.
    /// </summary>
    public
    sealed
    class StoreSimple
    {
        private StoreSimple() { }

        public
        static
        void RunStoreSimple(string connectionString)
        {
            Console.WriteLine("Running the Store Simple Sample");

            // Basic write blob. Uses the default store and default collection.
            //
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();

                // Create a blob context to operate againt RBS.
                //
                SqlRemoteBlobContext blobContext = new SqlRemoteBlobContext(conn);

                using (SqlRemoteBlob blob = blobContext.CreateNewBlob())
                {
                    // Store new blob data.
                    //
                    byte[] blobData = Encoding.Unicode.GetBytes("Remote Blob Storage Sample Application Store Simple Test Data");
                    blob.Write(blobData, 0, blobData.Length);

                    // Call commit to indicate we're done writing to the stream.
                    //
                    blob.Commit();

                    // Once all the data has been written to the blob, the next step is to register the blob 
                    // with the RBS auxiliary tables and insert the blob into the user application's metadata
                    // tables. This registration happens automatically when SqlRemoteBlob.GetBlobId is called.
                    // Make sure this registration and the application insert is wrapped in the same transaction.
                    //
                    using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
                    {
                        // Enlist the connection into the transaction.
                        //
                        conn.EnlistTransaction(Transaction.Current);

                        Console.WriteLine("Created Blob ID: {0}", BitConverter.ToString(blob.GetBlobId()));

                        // Add a new row including the blob ID to the database table.
                        //
                        string addBlobEntry = @" 
                                insert into 
                                    ApplicationSampleTable (SearchTags, BlobId) 
                                values 
                                    ('Sample, RBS, Demo', @blobId)
                            ";

                        using (SqlCommand cmd = new SqlCommand(addBlobEntry, conn))
                        {
                            cmd.Parameters.Add(new SqlParameter("@blobId", blob.GetBlobId()));
                            cmd.ExecuteNonQuery();
                        }
                        Console.WriteLine("Blob ID successfully stored in the application sample table.");
                        ts.Complete();
                    }
                }

                // Success
                //
            }
        }
    }
}
